package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentStockCkApply;
import com.mxpio.erp.common.equipment.entity.EquipmentStockCkApplyItem;
import com.mxpio.erp.common.equipment.entity.EquipmentStockCkHistory;
import com.mxpio.erp.common.equipment.entity.EquipmentStockItemInventory;
import com.mxpio.erp.common.equipment.service.EquipmentStockCkApplyItemService;
import com.mxpio.erp.common.equipment.service.EquipmentStockCkApplyService;
import com.mxpio.erp.common.equipment.service.EquipmentStockCkHistoryService;
import com.mxpio.erp.common.equipment.vo.VEquipmentStockCkHistoryDetail;
import com.mxpio.erp.common.equipment.vo.VEquipmentStockCkPosting;
import com.mxpio.erp.common.equipment.vo.VEquipmentStockInventory;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 备品出库
* @Author: jeecg-boot
* @Date:   2021-10-14
* @Version: V1.0
*/
@Tag(name = "EquipmentStockCkApplyController", description = "备品出库")
@RestController
@RequestMapping("/ck/tpmStockCkApply")
@Slf4j
public class EquipmentStockCkApplyController {

   @Autowired
   private EquipmentStockCkApplyService tpmStockCkApplyService;
   @Autowired
   private EquipmentStockCkApplyItemService tpmStockCkApplyItemService;
    @Resource
    private EquipmentStockCkHistoryService tpmStockCkHistoryService;

   /*---------------------------------主表处理-begin-------------------------------------*/

   /**
    * 分页列表查询
    * @param tpmStockCkApply
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @Operation(summary = "备品出库-分页列表查询", description = "备品出库-分页列表查询", method = "GET")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(EquipmentStockCkApply tpmStockCkApply,
                                  Criteria criteria,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       Pageable page = PageRequest.of(pageNo-1, pageSize);
       Page<EquipmentStockCkApply> pageList = tpmStockCkApplyService.listPage(EquipmentStockCkApply.class,page, criteria);
       return Result.OK(pageList);
   }

   /**
    *   添加
    * @param tpmStockCkApply
    * @return
    */
   @Operation(summary = "备品出库-添加", description = "备品出库-添加", method = "POST")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody EquipmentStockCkApply tpmStockCkApply) {
       tpmStockCkApply.setIsDel(0);
       tpmStockCkApply.setVersion(0);
       tpmStockCkApply.setStatus(0);
       tpmStockCkApplyService.save(tpmStockCkApply);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    * @param tpmStockCkApply
    * @return
    */
   @Operation(summary = "备品出库-编辑", description = "备品出库-编辑", method = "PUT")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody EquipmentStockCkApply tpmStockCkApply) {
       tpmStockCkApply.setVersion(tpmStockCkApply.getVersion()+1);
       tpmStockCkApplyService.update(tpmStockCkApply);
       return Result.OK("编辑成功!");
   }

    /**
     *  提交
     * @param tpmStockCkApply
     * @return
     */
    @Operation(summary = "备品出库-提交", description = "备品出库-提交", method = "PUT")
    @PutMapping(value = "/submit")
    public Result<?> submit(@RequestBody EquipmentStockCkApply tpmStockCkApply) {
        List<EquipmentStockCkApplyItem> itemList = tpmStockCkApplyItemService.selectByMainId(tpmStockCkApply.getId());
        if (itemList.size()<1){
            return Result.error("该出库单没有添加详情信息，不允许提交！");
        }
        return Result.OK(tpmStockCkApplyService.submit(tpmStockCkApply,itemList));
    }

   /**
    * 通过id删除
    * @param id
    * @return
    */
   @Operation(summary = "备品出库-通过id删除", description = "备品出库-通过id删除", method = "DELETE")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       tpmStockCkApplyService.delMain(id);
       return Result.OK("删除成功!");
   }

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @Operation(summary = "备品出库-批量删除", description = "备品出库-批量删除", method = "DELETE")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.tpmStockCkApplyService.delBatchMain(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

//   /**
//    * 导出
//    * @return
//    */
//   @RequestMapping(value = "/exportXls")
//   public ModelAndView exportXls(HttpServletRequest request, EquipmentStockCkApply tpmStockCkApply) {
//       return super.exportXls(request, tpmStockCkApply, EquipmentStockCkApply.class, "备品出库");
//   }
//
//   /**
//    * 导入
//    * @return
//    */
//   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//       return super.importExcel(request, response, EquipmentStockCkApply.class);
//   }
   /*---------------------------------主表处理-end-------------------------------------*/


   /*--------------------------------子表处理-出库单明细-begin----------------------------------------------*/
   /**
    * 通过主表ID查询
    * @return
    */
   @Operation(summary = "出库单明细-通过主表ID查询", description = "出库单明细-通过主表ID查询", method = "GET")
   @GetMapping(value = "/listEquipmentStockCkApplyItemByMainId")
   public Result<?> listEquipmentStockCkApplyItemByMainId(EquipmentStockCkApplyItem tpmStockCkApplyItem,
                                                          Criteria criteria,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
       Pageable page = PageRequest.of(pageNo-1, pageSize);
       Page<EquipmentStockCkApplyItem> pageList = tpmStockCkApplyItemService.queryByMainId(page, tpmStockCkApplyItem.getCkApplyId());
       return Result.OK(pageList);
   }

   /**
    * 添加
    * @param tpmStockCkApplyItem
    * @return
    */
   @Operation(summary = "出库单明细-添加", description = "出库单明细-添加", method = "POST")
   @PostMapping(value = "/addEquipmentStockCkApplyItem")
   public Result<?> addEquipmentStockCkApplyItem(@RequestBody EquipmentStockCkApplyItem tpmStockCkApplyItem) {
       tpmStockCkApplyItemService.saveItem(tpmStockCkApplyItem);
       return Result.OK("添加成功！");
   }

   /**
    * 编辑
    * @param tpmStockCkApplyItem
    * @return
    */
   @Operation(summary = "出库单明细-编辑", description = "出库单明细-编辑", method = "PUT")
   @PutMapping(value = "/editEquipmentStockCkApplyItem")
   public Result<?> editEquipmentStockCkApplyItem(@RequestBody EquipmentStockCkApplyItem tpmStockCkApplyItem) {
       tpmStockCkApplyItemService.updateItemById(tpmStockCkApplyItem);
       return Result.OK("编辑成功!");
   }

   /**
    * 通过id删除
    * @param id
    * @return
    */
   @Operation(summary = "出库单明细-通过id删除", description = "出库单明细-通过id删除", method = "DELETE")
   @DeleteMapping(value = "/deleteEquipmentStockCkApplyItem")
   public Result<?> deleteEquipmentStockCkApplyItem(@RequestParam(name="id",required=true) String id) {
       tpmStockCkApplyItemService.remove(EquipmentStockCkApplyItem.class,id);
       return Result.OK("删除成功!");
   }

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @Operation(summary = "出库单明细-批量删除", description = "出库单明细-批量删除", method = "DELETE")
   @DeleteMapping(value = "/deleteBatchEquipmentStockCkApplyItem")
   public Result<?> deleteBatchEquipmentStockCkApplyItem(@RequestParam(name="ids",required=true) String ids) {
       Criteria c = Criteria.create().addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
       this.tpmStockCkApplyItemService.removeBatch(EquipmentStockCkApplyItem.class,c);
       return Result.OK("批量删除成功!");
   }

   /**
    * 导出
    * @return
    *//*
   @RequestMapping(value = "/exportEquipmentStockCkApplyItem")
   public ModelAndView exportEquipmentStockCkApplyItem(HttpServletRequest request, EquipmentStockCkApplyItem tpmStockCkApplyItem) {
        // Step.1 组装查询条件
        QueryWrapper<EquipmentStockCkApplyItem> queryWrapper = QueryGenerator.initQueryWrapper(tpmStockCkApplyItem, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // Step.2 获取导出数据
        List<EquipmentStockCkApplyItem> pageList = tpmStockCkApplyItemService.list(queryWrapper);
        List<EquipmentStockCkApplyItem> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "出库单明细"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, EquipmentStockCkApplyItem.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("tpm_stock_ck_apply_item报表", "导出人:" + sysUser.getRealname(), "出库单明细"));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
   }

   *//**
    * 导入
    * @return
    *//*
   @RequestMapping(value = "/importEquipmentStockCkApplyItem/{mainId}")
   public Result<?> importEquipmentStockCkApplyItem(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<EquipmentStockCkApplyItem> list = ExcelImportUtil.importExcel(file.getInputStream(), EquipmentStockCkApplyItem.class, params);
                for (EquipmentStockCkApplyItem temp : list) {
                   temp.setCkApplyId(mainId);
                }
                long start = System.currentTimeMillis();
                tpmStockCkApplyItemService.saveBatch(list);
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.OK("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
   }
*/
   /*--------------------------------子表处理-出库单明细-end----------------------------------------------*/


    /*--------------------------------出库过账处理-start----------------------------------------------*/

    /**
     * 通过主表ID查询入库过账历史
     * @return
     */
    @Operation(summary = "入库过账历史-通过主表ID查询", description = "入库过账历史-通过主表ID查询", method = "GET")
    @GetMapping(value = "/ckApplyHistory")
    public Result<?> queryCkApplyHistoryByMainId(EquipmentStockCkApply tpmStockCkApply,
                                                 Criteria criteria,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentStockCkHistory> pageList = tpmStockCkHistoryService.queryCkApplyHistoryByMainId(page, tpmStockCkApply.getId());
        return Result.OK(pageList);
    }
    /**
     * 通过主表ID查询入库过账历史详情
     * @return
     */
    @Operation(summary = "入库过账历史详情-通过主表ID查询", description = "入库过账历史详情-通过主表ID查询", method = "GET")
    @GetMapping(value = "/ckApplyHistoryDetail")
    public Result<?> queryCkApplyHistoryDetailByMainId(EquipmentStockCkHistory tpmStockCkHistory,
                                                       Criteria criteria,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<VEquipmentStockCkHistoryDetail> pageList = tpmStockCkHistoryService.queryCkApplyHistoryDetailByMainId(page, tpmStockCkHistory);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     * @param tpmStockCkApply
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @Operation(summary = "备品出库过账-分页列表查询", description = "备品出库过账-分页列表查询", method = "GET")
    @GetMapping(value = "/listPosting")
    public Result<?> queryPageListPosting(EquipmentStockCkApply tpmStockCkApply,
                                          Criteria criteria,
                                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                          HttpServletRequest req) {
        criteria.addCriterion("status", Operator.EQ, 1);
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentStockCkApply> pageList = tpmStockCkApplyService.listPage(EquipmentStockCkApply.class,page, criteria);
        return Result.OK(pageList);
    }

    /**
     * 通过主表ID查询
     * @return
     */
    @Operation(summary = "出库单待入账详情-通过主表ID查询", description = "出库单待入账详情-通过主表ID查询", method = "GET")
    @GetMapping(value = "/queryCkPostingItemByMainId")
    public Result<?> queryCkPostingItemByMainId(EquipmentStockCkApplyItem tpmStockCkApplyItem,
                                                Criteria criteria,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentStockCkApplyItem> pageList = tpmStockCkApplyItemService.queryCkPostingByMainId(page, tpmStockCkApplyItem.getCkApplyId());
        return Result.OK(pageList);
    }

    /**
     * 查询库位
     *
     * @param tpmStockItemInventory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @Operation(summary = "库存信息-分页列表查询", description = "库存信息-分页列表查询", method = "GET")
    @GetMapping(value = "/listInventory")
    public Result<?> queryPageListInventory(EquipmentStockItemInventory tpmStockItemInventory,
                                            Criteria criteria,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<VEquipmentStockInventory> pageList = tpmStockCkApplyItemService.queryPageListInventory(page, tpmStockItemInventory.getMaterialsId());
        return Result.OK(pageList);
    }

    /**
     * 出库过账
     * @param tpmStockckPostingVo
     * @return
     */
    @Operation(summary = "出库过账", description = "出库过账", method = "POST")
    @PostMapping(value = "/ckPosting")
    public Result<?> ckPosting(@RequestBody VEquipmentStockCkPosting tpmStockckPostingVo) {
        tpmStockCkApplyService.ckPosting(tpmStockckPostingVo);
        return Result.OK("操作成功！");
    }
    /**
     * 出库过账
     * @param tpmStockckPostingVo
     * @return
     */
    @Operation(summary = "出库过账", description = "出库过账", method = "POST")
    @PostMapping(value = "/ckReject")
    public Result<?> ckReject(@RequestBody VEquipmentStockCkPosting tpmStockckPostingVo) {
        tpmStockCkApplyService.ckReject(tpmStockckPostingVo);
        return Result.OK("操作成功！");
    }

}
